/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.hargaitembiaya;

import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassSession;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.perekamanitembiaya.PerekamanItemBiayaDAO;
import org.sadhar.sipp.perekamanitembiaya.PerekamanItemBiayaDAOImpl;
import org.sadhar.sipp.posbiaya.PosBiayaDAO;
import org.sadhar.sipp.posbiaya.PosBiayaDAOImpl;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Space;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author bloodrunk
 */
public class HargaItemBiayaWnd extends ClassApplicationModule {

    private Textbox txtboxKeySearch;
    private Textbox txtboxIdPosBiaya;
    private Label lblTahunAnggaran;
    private Button btnCari;
    private Button btnSimpan;
    private Listbox lstboxItemBiaya;
    private Listbox lstboxDaftarItemBiaya;
    private Label lblIdPosBiaya;
    private Label lblNamaPosBiaya;
    private PosBiayaDAO posBiayaDAO;
    private HargaItemBiayaDAO hargaItemBiayaDAO;
    private PerekamanItemBiayaDAO perekamanItemBiayaDAO;
    private Boolean simpan = true;
    private ClassSession session;

    public HargaItemBiayaWnd() throws Exception {
        posBiayaDAO = new PosBiayaDAOImpl();
        hargaItemBiayaDAO = new HargaItemBiayaDAOImpl();
        perekamanItemBiayaDAO = new PerekamanItemBiayaDAOImpl();

    }

    public void onCreate() throws Exception {
        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        txtboxIdPosBiaya = (Textbox) getFellow("txtboxIdPosBiaya");
        lblTahunAnggaran = (Label) getFellow("lblTahunAnggaran");
        lblNamaPosBiaya = (Label) getFellow("lblNamaPosBiaya");
        lstboxItemBiaya = (Listbox) getFellow("lstboxItemBiaya");
        lstboxDaftarItemBiaya = (Listbox) getFellow("lstboxDaftarItemBiaya");
        lblIdPosBiaya = (Label) getFellow("lblNamaPosBiaya");
        btnCari = (Button) getFellow("btnCari");
        btnSimpan = (Button) getFellow("btnSimpan");
        session = new ClassSession("2009", "");
        lblTahunAnggaran.setValue(session.getThnAnggaran());
        load();
    }

    public void load() throws Exception {
        int no = 0;
        lstboxDaftarItemBiaya.getItems().clear();
        for (final Map itemBiaya : hargaItemBiayaDAO.listDetail(lblTahunAnggaran.getValue())) {
            Listitem item1 = new Listitem();
            item1.setValue(itemBiaya);

            item1.appendChild(new Listcell(itemBiaya.get("tahun").toString()));
            item1.appendChild(new Listcell(itemBiaya.get("namaPosBiaya").toString()));
            item1.appendChild(new Listcell(itemBiaya.get("namaItem").toString()));
            item1.appendChild(new Listcell(itemBiaya.get("harga").toString()));

            Listcell listcell = new Listcell();
            if (!ClassAntiNull.AntiNullString(itemBiaya.get("tahun")).trim().equalsIgnoreCase("")) {
                Toolbarbutton btnUbah = new Toolbarbutton();
                btnUbah.setId("btnUbah" + (no++));
                btnUbah.setLabel("[ubah]");
                btnUbah.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event arg0) throws Exception {

                        simpan = false;
                        lblIdPosBiaya.setValue(itemBiaya.get("namaPosBiaya").toString());
                        txtboxIdPosBiaya.setValue(itemBiaya.get("kodePosBiaya").toString());
                        txtboxKeySearch.setValue(itemBiaya.get("namaPosBiaya").toString());
                        txtboxKeySearch.setDisabled(true);
                        lstboxItemBiaya.getItems().clear();
                        int no = 1;
                        int indexHarga = 0;

                        for (final Map hargaItemBiaya : hargaItemBiayaDAO.listByPosBiaya(Integer.valueOf(itemBiaya.get("kodePosBiaya").toString()),lblTahunAnggaran.getValue())) {
                            Listcell listcell = new Listcell();
                            Listitem item2 = new Listitem();
                            item2.setValue(hargaItemBiaya);
                            item2.appendChild(new Listcell("" + no++));
                            item2.appendChild(new Listcell(hargaItemBiaya.get("namaItem").toString()));

                            Textbox txtboxHarga = new Textbox();
                            txtboxHarga.setId("txtboxHarga" + indexHarga++);
                            txtboxHarga.setValue(hargaItemBiaya.get("harga").toString());
                            txtboxHarga.setDisabled(false);
                            System.out.println(txtboxHarga.getId());

                            listcell.appendChild(txtboxHarga);
                            item2.appendChild(listcell);
                            lstboxItemBiaya.appendChild(item2);
                        }
                    }
                });
                listcell.appendChild(btnUbah);
                listcell.appendChild(new Space());
                item1.appendChild(listcell);
            }
            lstboxDaftarItemBiaya.appendChild(item1);
        }
    }

    public void lstboxDaftarItemBiayaOnSelect() throws Exception {
        lstboxItemBiaya.getItems().clear();
        int no = 1;
        int indexHarga = 0;
        Map map = (Map) lstboxDaftarItemBiaya.getSelectedItem().getValue();
        for (final Map hargaItemBiaya : hargaItemBiayaDAO.listByPosBiaya(map.get("namaPosBiaya").toString())) {
            Listcell listcell = new Listcell();
            Listitem item2 = new Listitem();
            item2.setValue(hargaItemBiaya);
            item2.appendChild(new Listcell("" + no++));
            item2.appendChild(new Listcell(hargaItemBiaya.get("namaItem").toString()));

            Textbox txtboxHarga = new Textbox();
            txtboxHarga.setId("txtboxHarga" + indexHarga++);
            txtboxHarga.setValue(hargaItemBiaya.get("harga").toString());
            txtboxHarga.setDisabled(true);

            listcell.appendChild(txtboxHarga);
            item2.appendChild(listcell);
            lstboxItemBiaya.appendChild(item2);
        }

    }

    public void btnCariOnClick() throws InterruptedException {
        final Window win = (Window) Executions.createComponents("sippzul/perekamanhargaitembiaya/popup.zul", this, null);
        Textbox txtboxSearch = (Textbox) win.getFellow("txtboxSearch");
        txtboxSearch.setValue(txtboxKeySearch.getValue());
        win.doModal();
    }

    public void btnSimpanOnClick() throws InterruptedException, Exception {
            if (txtboxIdPosBiaya.getValue().isEmpty()) {
                Messagebox.show("Pilih Pos Biaya !", "Peringatan", Messagebox.OK, Messagebox.ERROR);
            } else {
                int index = 0;
                //List<Map> result = perekamanItemBiayaDAO.select(Integer.valueOf(txtboxIdPosBiaya.getValue().toString()));
                List<Map> result = hargaItemBiayaDAO.listByPosBiaya(Integer.valueOf(txtboxIdPosBiaya.getValue().toString()),lblTahunAnggaran.getValue());
                try {
                    if (result != null) {
                        for (Map m : result) {
                            Textbox txtboxHarga = (Textbox) getFellow("txtboxHarga" + index++);
                            if (txtboxHarga.getValue().isEmpty()) {
                                throw new Exception();
                            }
                        }
                    }
                    int newIndex = 0;
                    for (Map m : result) {
                        HargaItemBiaya hargaItemBiaya = new HargaItemBiaya();
                        Textbox txtboxHarga = (Textbox) getFellow("txtboxHarga" + newIndex++);
                        hargaItemBiaya.setTahun(lblTahunAnggaran.getValue().trim());
                        hargaItemBiaya.setKodeItem(Integer.valueOf(m.get("kodeItem").toString()));
                        hargaItemBiaya.setHarga(Float.valueOf(txtboxHarga.getValue().trim()));
                        if (hargaItemBiayaDAO.cariHarga(hargaItemBiaya).size() <= 0) {
                            hargaItemBiayaDAO.insert(hargaItemBiaya);
                        }else{
                            hargaItemBiayaDAO.update(hargaItemBiaya);
                        }

                        System.out.println(txtboxHarga.getValue().trim());
                    }

                    load();
                    clearlistboxItemBiaya();
                    Messagebox.show("Data Telah Di Simpan", "Informasi", Messagebox.OK, Messagebox.INFORMATION);
                } catch (Exception e) {
                    System.out.println("Error : " + e.getMessage());
                    Messagebox.show("Masukan Harga !", "Pesan Kesalahan", Messagebox.OK, Messagebox.ERROR);
                    return;
                }
            }
    }

    public void btnBatalOnClick() {
        clearlistboxItemBiaya();
        txtboxKeySearch.setDisabled(false);
        simpan = true;
    }

    public void clearlistboxItemBiaya() {
        lstboxItemBiaya.getItems().clear();
        txtboxKeySearch.setValue("");
        txtboxKeySearch.setDisabled(false);
        lblNamaPosBiaya.setValue("");
        simpan = true;
    }
}
